#include "stdafx.h"
#include "libVideoSaver/cVideoSaveAvi.h"
#include "libAVI/cAviLib.h"
#include <string.h>
#include "cVideoSaveAvi.h"

cVideoSaveAvi::~cVideoSaveAvi()
{
    Uninit();
}

int cVideoSaveAvi::Init()
{
    Uninit();
    m_pHandle = new CAviLib();
    if(m_pHandle!= NULL)
    {
        return 0;
    }
    else
    {
        return -1;
    }
}

int cVideoSaveAvi::Uninit()
{
    if(m_pHandle != NULL)
    {
        CAviLib* pHandle = (CAviLib*)m_pHandle;
        pHandle->close();
        delete pHandle;
        m_pHandle = NULL;
    }
    return 0;
}

int cVideoSaveAvi::CreateVideoFile(const char *chFileName, int iVideoWidth, int iVideoHeight, int iFrameRate)
{
    if(m_pHandle == NULL)
        return -1;

    CAviLib* pHandle = (CAviLib*)m_pHandle;
    pHandle->setAviInfo((char*)chFileName, iVideoWidth, iVideoHeight, iFrameRate, "H264");
    return 0;
}

int cVideoSaveAvi::WriteH264Frame(bool bIframe, unsigned char *pbFrameData, int iFrameSize)
{
    if(m_pHandle == NULL)
        return -1;

    CAviLib* pHandle = (CAviLib*)m_pHandle;
    if(pHandle->IsNULL())
    {        
        return -2;
    }
    pHandle->writeFrame((char*)pbFrameData, iFrameSize, bIframe? 1 : 0);
    return 0;
}

int cVideoSaveAvi::CloseVideoFile()
{
    if(m_pHandle == NULL)
        return -1;

    CAviLib* pHandle = (CAviLib*)m_pHandle;
    pHandle->close();
    return 0;
}
